import { JXDef } from "../../../conventions/JXCommon";
import { OBJECT_COPY } from "../../../Core/CoreDefine";
import GViewBase from "../../../Core/GView/GViewBase";
import MathEx from "../../../Core/Math/MathEx";
import { Res } from "../../Common/UIResources";
import GameMgr from "../../Logic/GameMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HeroTryCtrl extends GViewBase {
	@property(cc.Sprite) heroDraw: cc.Sprite = null;
	@property(cc.Label) heroName: cc.Label = null;

	private _tryHeroData: SHeroTryDataRaw;
	private _sHeroData: SHeroDataRaw;
	private _cb: Function = null;
	onGStart(cb) {
		this._cb = cb;
		this.setTryHeroId();
	}

	protected setTryHeroId() {
		let arr: SHeroTryDataRaw[] = GameMgr.heroTryData.data.values();
		let filtArr: SHeroTryDataRaw[] = []
		for (let i = 0; i < arr.length; i++) {
			let suit = arr[i];
			if (suit) {
				let hero = GameMgr.rHeroData.getHero(suit.heroId);
				if (!hero) {
					filtArr.push(suit)
				}
			}
		}
		if (!filtArr.length) {
			filtArr = OBJECT_COPY(arr)
		}
		if (filtArr.length) {
			let index = MathEx.random(0, filtArr.length - 1)
			this._tryHeroData = filtArr[index]
			this._sHeroData = GameMgr.heroData.getRaw(this._tryHeroData.heroId);
			this.heroName.string = this._sHeroData.name;
			this.assetImpl.spriteFrame(this.heroDraw, Res.texture.hero.hero + "hero_" + this._sHeroData.icon);
		}


	}
	protected noTryClick() {
		GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.heroTry, { msg: "英雄试用不试" });
		GameMgr.rHeroData.tryHeroData = null;
		this.onClose();
	}

	protected tryClick() {
		GameMgr.sdkMgr.watchAd(() => {
			GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.heroTry, { msg: "英雄试用" });
			GameMgr.rHeroData.tryHeroData = this._tryHeroData;
			this.onClose();
		})
	}

	onClose() {
		super.onClose();
		this._cb();
	}
}